/******************************************************************************
 * (c) 2011 Komersi Software                                                  *
 * www.komersisoftware.com                                                    *
 *                                                                            *
 * This file is part of Tadah                                                 *
 * Licensed under GNU General Public License v2.0                             *
 * Check LICENSE.TXT for more information                                     *
 ******************************************************************************/
 
#include <string.h> 
#include <stdlib.h>
 
#include "genericobject.h"
#include "ddl.h"
#include "engine.h"

// TODO: parameter validations

void dispose_object(generic_object_t **object)
{
	if (object != NULL)
	{
		if (*object != NULL)
		{
			if ((*object)->value != NULL)
			{
				free((*object)->value);
				(*object)->value = NULL;
			}

			free((*object));
			(*object) = NULL;
		}
		
		object = NULL;
	}
}

void dispose_object_list(generic_object_t **list)
{
   generic_object_t *cur = *list;
   generic_object_t *aux = *list;

   if (list == NULL)
       return;

   while (cur != NULL)
   {
       aux = cur->next;
       dispose_object(&cur);
       cur = aux;
   }
}

generic_object_t *new_object(ddl_column_t *column)
{
	generic_object_t *obj = NULL;
	
	obj = malloc(sizeof(generic_object_t));
    if (obj == NULL) 
    	return NULL;
    	
    memset(obj, 0, sizeof(generic_object_t));
    	
    obj->value = NULL;
 	obj->next = NULL;
    obj->column = column;
    
    return obj;
}

void add_object(generic_object_t **row, generic_object_t *object)
{
	generic_object_t *cur = NULL;
	
    if (*row == NULL) 
    {
        *row = object;
        return;
    }
      
    cur = *row;
    while (cur->next != NULL)
        cur = cur->next;
   
    cur->next = object;
}

generic_object_t *add_new_object(generic_object_t **row, ddl_column_t *column)
{
    generic_object_t *obj = NULL;

    obj = new_object(column);
    add_object(row, obj);

    return obj; 
}

#ifndef _MSC_VER
inline
#endif
int get_object_value_int(generic_object_t *object)
{
    return *((int *) object->value);
}

#ifndef _MSC_VER
inline
#endif
char *get_object_value_char(generic_object_t *object)
{
    return ((char *) object->value);
}

int set_object_value_int(generic_object_t *object, int value)
{
   int *ptr = NULL;
   ptr = malloc(sizeof(int));
   if (ptr == NULL) return FAILURE;

   memcpy(ptr, &value, sizeof(int));

   object->value = ptr;

   return SUCCESS;
}

#ifndef _MSC_VER
inline
#endif
void set_object_value_ptr(generic_object_t *object, void *ptr)
{
    object->value = ptr;
}

int set_object_value_char(generic_object_t *object, char *value, int size)
{
   char *ptr = NULL;
   ptr = calloc(size + 1, sizeof(char));
   if (ptr == NULL) return FAILURE;

   memcpy(ptr, value, strlen(value));

   object->value = ptr;

   return SUCCESS;
}

generic_object_t *new_test_row(int id, char name[100], ddl_column_t *list)
{
   generic_object_t *row = NULL;
   ddl_column_t *name_col = ddl_column_from_name(list, "name");
   
   generic_object_t *id_obj = add_new_object(&row, ddl_column_from_name(list, "id"));
   generic_object_t *name_obj = add_new_object(&row, name_col);

   set_object_value_int(id_obj, id);
   set_object_value_char(name_obj, name, name_col->size);

   return row;
}

generic_object_t *object_from_column(generic_object_t *list, ddl_column_t *column)
{
   generic_object_t *cur = list;
   while (cur != NULL)
   {
      if (cur->column == column)
         return cur;
         
      cur = cur->next;
   }
   
   return NULL;
}
